package com.ds.esd.formula.service;

import com.ds.bpm.formula.ParticipantSelect;
import com.ds.common.JDSException;
import com.ds.common.util.StringUtility;
import com.ds.config.ErrorResultModel;
import com.ds.config.ResultModel;
import com.ds.enums.db.MethodChinaName;
import com.ds.esb.config.formula.FormulaParams;
import com.ds.esd.bpm.BPMFactory;
import com.ds.esd.client.ESDFacrory;
import com.ds.esd.custom.api.annotation.APIEventAnnotation;
import com.ds.esd.custom.api.enums.CustomCallBack;
import com.ds.esd.custom.enums.CustomMenuItem;
import com.ds.esd.project.config.FormulaInst;
import com.ds.esd.project.config.FormulaInstParams;
import com.ds.esd.project.config.ProjectConfig;
import com.ds.org.Role;
import com.ds.org.RoleNotFoundException;
import com.ds.server.OrgManagerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping(path = "/admin/formula/component/role/")
@MethodChinaName(cname = "角色", imageClass = "bpmfont bpm-gongzuoliu-moxing")

public class RoleFService {


    @RequestMapping("addRole")
    @APIEventAnnotation(bindMenu = CustomMenuItem.treeSave, callback = {CustomCallBack.ReloadParent, CustomCallBack.Close})
    @ResponseBody
    public ResultModel<Boolean> addRole(String projectName, String RolePopTree, String RolesPopTree, String formulaInstId, String parameterCode, FormulaParams paramsType) {
        ResultModel<Boolean> model = new ResultModel<>();
        try {
            ProjectConfig config = ESDFacrory.getESDClient().getProjectByName(projectName).getConfig();
            FormulaInst inst = config.getFormulas().get(formulaInstId);
            ParticipantSelect select = BPMFactory.getInstance(ESDFacrory.getESDClient().getSpace()).getFormulaById(inst.getParticipantSelectId());
            FormulaInstParams params = (FormulaInstParams) inst.getParamsMap().get(parameterCode);
            if (params == null) {
                params = new FormulaInstParams();
                params.setFormulaInstId(inst.getFormulaInstId());
                params.setParameterCode(parameterCode);
                params.setParameterName(parameterCode);
                if (paramsType != null && !paramsType.equals("")) {
                    params.setParamsType(paramsType);
                }
                inst.getParamsMap().put(parameterCode, params);
            }

            if (RolesPopTree != null && !RolesPopTree.equals("")) {
                String[] roleIds = StringUtility.split(RolesPopTree, ";");
                for (String roleId : roleIds) {
                    try {
                        Role role = OrgManagerFactory.getOrgManager().getRoleByID(roleId);
                        params.getParameterValues().put(roleId, role.getName());
                    } catch (RoleNotFoundException e) {
                        e.printStackTrace();
                    }
                }
            } else if (RolePopTree != null && !RolePopTree.equals("")) {
                try {
                    Role role = OrgManagerFactory.getOrgManager().getRoleByID(RolePopTree);
                    params.getParameterValues().put(RolePopTree, role.getName());
                } catch (RoleNotFoundException e) {
                    e.printStackTrace();
                }
            }

            ESDFacrory.getESDClient().getProjectByName(projectName).updateConfig(config);
        } catch (JDSException e) {
            model = new ErrorResultModel();
            ((ErrorResultModel<Boolean>) model).setErrdes(e.getMessage());
        }


        return model;
    }


    @RequestMapping("delRole")
    @APIEventAnnotation(bindMenu = CustomMenuItem.delete, callback = {CustomCallBack.Reload})
    @ResponseBody
    public ResultModel<Boolean> delRole(String formulaInstId, String parameterCode, String projectName ,String id) {
        ResultModel<Boolean> model = new ResultModel<>();
        try {
            ProjectConfig config = ESDFacrory.getESDClient().getProjectByName(projectName).getConfig();
            FormulaInst inst = config.getFormulas().get(formulaInstId);
            FormulaInstParams params = (FormulaInstParams) inst.getParamsMap().get(parameterCode);
            String[] roleIds = StringUtility.split(id, ";");

            for (String roleId : roleIds) {
                params.getParameterValues().remove(roleId);
            }
            ESDFacrory.getESDClient().getProjectByName(projectName).updateConfig(config);
        } catch (JDSException e) {
            model = new ErrorResultModel();
            ((ErrorResultModel<Boolean>) model).setErrdes(e.getMessage());
        }


        return model;
    }

    private FormulaInst getFormula(String projectId, String formulaInstId) throws JDSException {
        ProjectConfig config = ESDFacrory.getESDClient().getProjectById(projectId).getConfig();
        FormulaInst formulaInst = config.getFormulas().get(formulaInstId);
        return formulaInst;
    }


}
